﻿using LY.DataAccess.Repository;
using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using LY.Interface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace LY.Business.ServiceImpl
{
    public class MenuService : IMenuService
    {
          MenuRepository _repo;
          public MenuService()
        {
            _repo = new MenuRepository(); 
        }
          public PagedList<Menu_EX> QueryMenuList(MenuSearch search, PageView view)
        {
            return _repo.QueryMenuList(search,view);
        }

          public List<C_Menu> GetUpperMenuList(int? id)
          {
              return _repo.GetUpperMenuList(id);
          }

          public C_Menu GetMenu(int? id)
          {
              return _repo.GetMenu(id);
          }

          public DataView GetMenuView(int? id) 
          {
              return _repo.GetMenuView(id);
          }

          public List<C_Menu> GetMenuList(int? UpperID)
          {
              return _repo.GetMenuList(UpperID);
          }

          public int DeleteMenu(int id)
          {
              return _repo.DeleteMenu(id);
          }

          public string GetCode(string code)
          {
              return _repo.GetCode(code);
          }
          public List<C_Menu> GetTopMenuList(int? id)
          {
              return GetTopMenuList(id);
          }

          /// <summary>
          /// 根据栏目Id查询子栏目信息
          /// </summary>
          /// <param name="id"></param>
          /// <param name="view"></param>
          /// <returns></returns>
          public PagedList<C_Menu> QueryMenuListId(int? Id, ArticleSearch search, PageView view)
          {
              return _repo.QueryMenuListId(Id, search, view);
          }

          /// <summary>
          /// 检索栏目信息
          /// </summary>
          /// <param name="search"></param>
          /// <param name="view"></param>
          /// <returns></returns>
          public PagedList<C_Menu> QueryMenuList(ArticleSearch search, PageView view)
          {
              return _repo.QueryMenuList(search, view);
          }

          /// <summary>
          /// 检索模块信息
          /// </summary>
          /// <param name="id"></param>
          /// <returns></returns>
          public List<C_Menu> GetMenuSub(int? id)
          {
              return _repo.GetMenuSub(id);
          }

          /// <summary>
          /// 检查Code是否已经存在
          /// </summary>
          /// <param name="code"></param>
          /// <returns></returns>
          public int GetCodeNum(string code)
          {
              return _repo.GetCodeNum(code);
          }

          /// <summary>
          /// 修改时，检查Code是否存在,不包含当前Code
          /// </summary>
          /// <param name="id"></param>
          /// <param name="code"></param>
          /// <returns></returns>
          public int GetCodeNum(int id, string code)
          {
              return _repo.GetCodeNum(id, code);
          }

          /// <summary>
          /// 根据栏目ID查找栏目下是否有文章信息
          /// </summary>
          /// <param name="menuid"></param>
          /// <returns></returns>
          public int CheckArticle(int menuid)
          {
              return _repo.CheckArticle(menuid);
          }

        /// <summary>
        /// 前台栏目列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
          public List<C_Menu> QueryMainMenuList(int id)
          {
              return _repo.GetUpperMenuList(id);
          }

          /// <summary>
          /// 前台栏目列表
          /// </summary>
          /// <param name="id"></param>
          /// <returns></returns>
          public List<C_Menu> QueryMainMenuListByMainMenuId(int id)
          {
              return _repo.GetUpperMenuListByMainId(id);
          }

          /// <summary>
          /// 前台栏目ID
          /// </summary>
          /// <param name="id"></param>
          /// <returns></returns>
          public C_Menu GetMenuByMainMenuId(int id)
          {
              return _repo.GetMenuByMainMenuId(id);
          }

          /// <summary>
          /// 新增或修改时保存栏目信息
          /// </summary>
          /// <param name="article"></param>
          /// <returns></returns>
          public int SaveMenu(C_Menu menu)
          {

              string oldpath = "";
              string newpath = "";
              int menuSequence = 0;
              if (menu.Sequence != null)
              {
                  menuSequence = Convert.ToInt32(menu.Sequence);
              }
              if (menu.ID <= 0)
              {
                  //新增
                  //获取父级资料
                  int ParentID = Convert.ToInt32(menu.UpperID);
                  C_Menu parentmodule = _repo.GetMenu(ParentID) ?? new C_Menu
                      {
                          Layer = 0,
                          Path = ""
                      }; 
                  menu.Layer =  parentmodule.Layer + 1 ;   
                  menu.Path = parentmodule.Path + Convert.ToString(menu.ID + menuSequence * 10000 + 100000000) + ',';
                  return (int)_repo.Insert(menu);
              }
              else
              {
                  //编辑
                  oldpath = menu.Path;
                  //获取父级资料
                  int ParentID = Convert.ToInt32(menu.UpperID);
                  C_Menu parentmodule = _repo.GetMenu(ParentID) ?? new C_Menu
                  {
                      Layer = 0,
                      Path = ""
                  }; 
                  menu.Layer = parentmodule.Layer + 1;
                  newpath = parentmodule.Path + Convert.ToString(menu.ID + menuSequence * 10000 + 100000000) + ',';

                  UpdateSequence(oldpath, newpath);
                  menu.Path = newpath;
                  return (int)_repo.Update(menu);

              }
          }

          public int UpdateSequence(string oldpath, string newpath)
          {
              return _repo.UpdateSequence(oldpath, newpath);
          }


          /// <summary>
          /// 根据当前菜单的Id获取母菜单，目前只适用于2级菜单。
          /// </summary>
          /// <param name="id"></param>
          /// <returns></returns>
          public C_Menu SelectUpperMenuById(int id)
          {
              return _repo.SelectUpperMenuById(id);
          }
    }
}
